package atom.core3.spring.web.interceptor;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import atom.core2.j2ee.util.RequestManager;
import atom.core2.j2ee.util.SessionManager;

//interceptor不是线程安全的 我们需要借助threadlocal来实现线程安全
public class TokenInterceptor implements HandlerInterceptor
{
	// 日志
	private static Logger logger = Logger.getLogger(TokenInterceptor.class);
	// 方法调用前调用
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
	{
		//log.debug("before the handle invoke :" + new Date());
		boolean bValieToken = SessionManager.isValidTokenKey(request);
		if (!bValieToken)
		{
			
			request.setAttribute("result", "N");
			request.setAttribute("message", "不允许重复提交数据.");
			RequestManager.forward(request, response, "/pages_public/common/error.jsp");
		
		}
		return true;
	}

	// 方法调用时，在view render之前的调用
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView view)
	        throws Exception
	{
		logger.debug("invoke method,but before render view : " + new Date());
	}

	// 调用结束后，已经view render了之后的clean操作
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
	        throws Exception
	{
		
		logger.debug("after render the view :" + new Date());
		
	}


}
